package com.carl.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.carl.sysutil.StringUtil;

public class JspFilter extends HttpServlet implements Filter {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1695724027245516436L;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
		
		HttpServletRequest req = (HttpServletRequest) request;  
		HttpServletResponse res = (HttpServletResponse) response;  
		HttpSession session = req.getSession(true);
		
		 String username = (String) session.getAttribute("userName");
		 
		 if(!StringUtil.isNotBlankEmpty(username)){
			 
			 dispatcher.forward(request,response);
			 res.setHeader("Cache-Control","no-store");     
			 res.setDateHeader("Expires",0);  
			 res.setHeader("Pragma","no-cache");  
			 
		 }else{
			 chain.doFilter(request,response);  
		 }
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

}
